﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    class Input_Constraint
    {
        static public TextBox keepOnly(TextBox input, bool letters, bool digits, bool spaces, bool puncutation, List<char> keepOthers)
        {
            int pos = input.SelectionStart;
            int oldSize = input.Text.Length;

            input.Text = Input_Constraint.keepOnly(input.Text, true, true, false, false, null);

            int newSize = input.Text.Length;

            input.SelectionStart = pos - oldSize + newSize + 1;

            return input;
        }

        static public RichTextBox keepOnly(RichTextBox input, bool letters, bool digits, bool spaces, bool puncutation, List<char> keepOthers)
        {
            int pos = input.SelectionStart;
            int oldSize = input.Text.Length;

            input.Text = Input_Constraint.keepOnly(input.Text, true, true, false, false, null);

            int newSize = input.Text.Length;

            input.SelectionStart = pos - oldSize + newSize;

            return input;
        }

        static public TextBox phoneNumber(TextBox input)
        {
            int pos = input.SelectionStart;
            int oldSize = input.Text.Length;

            input.Text = phoneNumber(input.Text);

            int newSize = input.Text.Length;

            input.SelectionStart = pos - oldSize + newSize;

            return input;
        }

        static public string keepOnly(string input, bool letters, bool digits, bool spaces, bool punctuation, List<char> keepOthers)
        {
            string output = "";

            foreach (char c in input.ToCharArray())
            {
                if (letters && char.IsLetter(c))
                {
                    output += c;
                    continue;
                }

                if (digits && char.IsDigit(c))
                {
                    output += c;
                    continue;
                }

                if (spaces && c == ' ')
                {
                    output += c;
                    continue;
                }

                if (punctuation && char.IsPunctuation(c))
                {
                    output += c;
                    continue;
                }

                if (keepOthers != null)
                {
                    foreach (char other in keepOthers)
                    {
                        if (other == c)
                        {
                            output += c;
                            break;
                        }
                    }
                }
            }

            return output;
        }

        static public string keepOnly(string input, bool letters, bool digits, bool spaces, bool punctuation, List<char> keepOthers, List<char> remove)
        {
            input = keepOnly(input, letters, digits, spaces, punctuation, keepOthers);

            string output = "";

            foreach (char c in input)
            {
                bool keep = true;

                foreach (char item in remove)
                {
                    if (item == c)
                    {
                        keep = false;
                        break;
                    }
                }

                if (keep)
                {
                    output += c;
                }
            }
            return output;
        }

        static public string phoneNumber(string input)
        {
            string output = "";
            int outputLength = 0;

            foreach (var c in input.ToCharArray())
	        {
                if (outputLength >= 12)
                {
                    break;
                }

                if (outputLength == 3 || outputLength == 7)
                {
                    output += '-';
                    outputLength++;
                }

                if (char.IsDigit(c))
                {
                    output += c;
                    outputLength++;
                }
            }

            return output;
        }
    }
}
